package com.example.springbott.wx_service;

import com.example.springbott.bean.MarketComment;
import com.example.springbott.bean.MarketCommentExample;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.mapper.MarketCommentMapper;
import com.example.springbott.mapper.MarketUserMapper;
import com.example.springbott.vo.WxCommentCount;
import com.example.springbott.vo.WxCommentList;
import com.example.springbott.vo.WxCommentUserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 付常莹
 * @description: TODO
 * @date 2022/11/22 21:16
 *
 */
@Service
public class WxCommentServiceIml implements WxCommentService {
    @Resource
    MarketCommentMapper marketCommentMapper;

    @Resource
    MarketUserMapper marketUserMapper;

    Integer hasPic = 1;

    /**
     * 该方法用于用户新增留言
     *
     * @param marketComment
     * @return
     */
    @Override
    public MarketComment post(MarketComment marketComment) {

        long l = System.currentTimeMillis();
        Date date = new Date(l);
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);
        marketCommentMapper.insertSelective(marketComment);
        MarketComment data = marketCommentMapper.selectByPrimaryKey(marketComment.getId());
        return data;
    }


    @Override
    public WxCommentCount count(Integer valueId, Integer type) {
//        MarketCommentExample commentExample = new MarketCommentExample();
//        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
//        long l = marketCommentMapper.countByExample(commentExample);

        int i = marketCommentMapper.countComment(valueId, type);
        int i1 = marketCommentMapper.countPic(valueId, type);

        WxCommentCount count1 = new WxCommentCount(i, i1);
        return count1;
    }


    @Override
    public CommonData<WxCommentList> list(Integer valueId, Integer type, Integer showType, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<WxCommentList> commentLists = marketCommentMapper.selectByValueId(valueId);
//        ArrayList<WxCommentList> commentLists1 = new ArrayList<>();
        Integer[] ids = marketCommentMapper.selectIdByValueId(valueId);
        for (WxCommentList commentList : commentLists) {
            //根据valueId查出评论该商品的用户的id
            for (Integer id : ids) {
                WxCommentUserInfo userInfo = marketUserMapper.selectById(id);
                commentList.setUserInfo(userInfo);
            }
//            commentLists.add(commentList);
        }
        PageInfo<WxCommentList> pageInfo = new PageInfo<>(commentLists);
        long total = pageInfo.getTotal();
        int page1 = pageInfo.getPageNum();
        int pages = pageInfo.getPages();
        int limit1 = pageInfo.getPageSize();
        List<WxCommentList> list = pageInfo.getList();
        CommonData<WxCommentList> data = new CommonData<>((int) total, pages, limit1, page1, list);
        return data;
    }
}


